package cn.xllyd.service.impl;

import cn.xllyd.domain.Order;
import cn.xllyd.domain.OrderDriver;
import cn.xllyd.exception.GlobalExceptionCodeEnum;
import cn.xllyd.mapper.OrderMapper;
import cn.xllyd.result.JSONResult;
import cn.xllyd.service.IOrderDriverService;
import cn.xllyd.service.IOrderService;
import cn.xllyd.utils.AssertUtil;
import cn.xllyd.utils.CodeGenerateUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author mylxx
 * @since 2022-12-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private IOrderDriverService orderDriverService;

    @Override
    public JSONResult inOrder(Order order) {
        // 参数校验

        // 业务校验
        AssertUtil.isNotNull(order, GlobalExceptionCodeEnum.ERROR);
        order.setOrderNum(CodeGenerateUtils.generateOrderSn(order.getId())); // 生成随机编号
        order.setOrderWeight(new BigDecimal(10));
        order.setOrderCreate(new Date());
        order.setOrderDistant(500L);
        order.setOrderStatus(1);
        order.setOrderCurrency(new BigDecimal(order.getOrderDistant()*5));
        return JSONResult.success(insert(order));
    }

    @Override
    public Object getOrder(String orderNum) {
        OrderDriver orderDriver = new OrderDriver();
        orderDriver.setOrderNum(orderNum);
        orderDriver.setDriverId(1L);
        orderDriver.setCreateDate(new Date());
        orderDriver.setState(1);
        return orderDriverService.insert(orderDriver);
    }
}
